/*!
 * Version: 0.1.0
 * Author: Boring
 * Update: 2015-08-20 11:40
 */

(function () {
	var
		getByClass = function (className, container) {
			var container = container || document,
				allElms,
				elms,
				i;

			if (container.getElementsByClassName) {
				elms = container.getElementsByClassName(className);
			} else if (container.querySelector) {
				elms = container.querySelectorAll('.' + className);
			}

			return elms;
		},

		forEach = function (array, handler) {
			var i;

			if (array.forEach) {
				array.forEach(function (item) {
					handler(item);
				});
			} else {
				for (i = 0; i < array.length; i++) {
					handler(array[i], i);
				}
			}
		},

		filter = function (array, handler) {
			var newArray = [],
				i;

			if (array.filter) {
				newArray = array.filter(function (item) {
					return handler(item);
				});
			} else {
				forEach(array, function (item) {
					if (handler(item)) {
						newArray.push(item);
					}
				});
			}

			return newArray;
		},

		listen = function (elm, type, handler, capture) {
			if (elm.addEventListener) {
				elm.addEventListener(type, handler, capture || false);
			} else if (elm.attachEvent) {
				elm.attachEvent('on' + type, handler);
			}
		},

		getClassList = function (elm) {
			return elm.classList || {
				add: function (className) {
					elm.className += " " + className;
				},

				remove: function (className) {
					if (this.contains(className)) {
						elm.className = elm.className.replace(className, "");
					}
				},

				toggle: function (className) {
					if (this.contains(className)) {
						this.remove(className);
					} else {
						this.add(className);
					}
				},

				contains: function (className) {
					return elm.className.indexOf(className) !== -1;
				}
			};
		},

		tab = function (container, config) {
			var
				init = function () {
					listenTrigger();
					go(current);
				},

				listenTrigger = function () {
					forEach(handles, function (handle, index) {
						listen(handle, eventType, function () {
							go(index);
						});
					});
				},

				go = function (index) {
					clearCurrentStatus();
					addTargetStatus(index);

					current = index;
				},

				clearCurrentStatus = function () {
					getClassList(handles[current]).remove(currentHandleClassName);
					getClassList(panels[current]).remove(currentPanelClassName);
				},

				addTargetStatus = function (index) {
					getClassList(handles[index]).add(currentHandleClassName);
					getClassList(panels[index]).add(currentPanelClassName);
				},

				container = typeof container === 'string' ? document.getElementById(container) : container,
				config = config || {},
				handlesWrap = getByClass('boring-tab-handles', container)[0],
				handles = handlesWrap.children,
				panelsWrap = getByClass('boring-tab-panels', container)[0],
				panels = panelsWrap.children,
				eventType = config.event || 'click',
				current = 0,
				currentClassName = 'boring-tab-current',
				currentHandleClassName = config.currentHandleClassName || currentClassName,
				currentPanelClassName = config.currentPanelClassName || currentClassName;

			init();
		},

		lazyload = function (config) {
			var
				init = function () {
					loadImgsInView();
					listenScroll();
				},

				loadImgsInView = function () {
					var imgsInView = getImgsInView();

					forEach(imgsInView, function (imgInView) {
						var tempImg,
							src;

						if (imgInView.loadStarted) {
							return;
						}

						tempImg = document.createElement('img');
						src = imgInView.dataset.boringLazyloadSrc;

						imgInView.loadStarted = true;

						listen(tempImg, 'load', function () {
							imgInView.src = src;

							if (onload) {
								onload(imgInView);
							}
						});

						tempImg.src = src;
					});
				},

				getImgsInView = function () {
					return filter(imgs, function (img) {
						return isInview(img);
					});
				},

				isInview = function (img) {
					var imgRect = img.getBoundingClientRect();

					return imgRect.top < window.innerHeight && imgRect.bottom > 0;
				},

				listenScroll = function () {
					listen(window, 'scroll', loadImgsInView);
				},

				config = config || {},
				onload = config.onload,
				imgs = getByClass('boring-lazyload');

			init();
		},

		boring = {
			getByClass: getByClass,
			forEach: forEach,
			listen: listen,
			getClassList: getClassList,
			tab: tab,
			lazyload: lazyload
		};

	if (typeof window.define === 'function') {
		define(boring);
	}

	window.boring = boring;
}());